/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Beans.Actores;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Alex
 */
public class ActoresDao {
    
    Connection con=null;
    
    public void setConection( Connection con){
        this.con = con;
    }
    
    public boolean existe_Actores(Actores actores) throws SQLException
	 {
		ResultSet re=null;
		PreparedStatement ps=null;
		boolean bandera=false;
		try
		{
			ps= con.prepareStatement("Select * from actores where id_actor=?");
                        ps.setString(1, actores.getIdactor());
			re=ps.executeQuery();
			while(re.next())
			{
				bandera=true;
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return bandera;
	}
	
	//Adicionar elementos a la BD

	public void newActor(Actores actores) throws SQLException
	{
		if(existe_Actores(actores))
			throw new SQLException("El Actor Ya Existe");

		PreparedStatement pstmt = null;
		try
		{
			pstmt = con.prepareStatement("INSERT INTO actores VALUES(?, ?, ?, ?)");
                        
			pstmt.setString(1,actores.getIdactor());
			pstmt.setString(2,actores.getNombreactor());
			pstmt.setString(3,actores.getSexoactor());
                        pstmt.setString(4,actores.getRolactor());                   

			pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
	}
        
        public List listar()throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		List lista = new LinkedList();
		try
		{
			ps=(PreparedStatement) con.prepareStatement("Select * from actores");
                        re=ps.executeQuery();
			while(re.next())
			{
				lista.add(Actores.load(re));
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return lista;
        }
        
        
        public List buscar(int tipo, String parametro)throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		List lista = new LinkedList();
                String sql="";
		try
		{
                    if(tipo==1)
                        sql="Select * from actores where id_actor like ?"; 
                     if(tipo==2)
                        sql="Select * from actores where nom_actor like ?"; 
                    
			ps=(PreparedStatement) con.prepareStatement(sql);
                        ps.setString(1,parametro);
                        re=ps.executeQuery();
			while(re.next())
			{
				lista.add(Actores.load(re));
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return lista;
        }
        
        public void eliminar(String cod)throws SQLException{
                PreparedStatement pstmt = null;
		try
		{
			pstmt =con.prepareStatement("delete from actores where id_actor=?");
			pstmt.setString(1,cod);
			pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
        }
        
        public void modificar(Actores p)throws SQLException{
            

		PreparedStatement pstmt = null;
		try
		{
			pstmt =con.prepareStatement("Update actores set nom_actor = ?, sexo_actor = ?, rol_actor = ? where id_actor = ?");
			pstmt.setString(1,p.getNombreactor());
			pstmt.setString(2,p.getSexoactor());
                        pstmt.setString(3,p.getRolactor());
                        pstmt.setString(4,p.getIdactor());
                        pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
            
        }
        
        public Actores obtener(String cod)throws SQLException{
            ResultSet re=null;
		PreparedStatement ps=null;
		Actores p = new Actores();
		try
		{
			ps=(PreparedStatement) con.prepareStatement("Select * from actoes where id_actor=?");
                        re=ps.executeQuery();
			while(re.next())
			{
				p = Actores.load(re);
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return p;
        }
        
         public Actores getActores(String id)throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		Actores p = new Actores();
		try
		{
			ps=con.prepareStatement("Select * from actores where id_actor=?");
                        ps.setString(1, id);
                        re=ps.executeQuery();
			if(re.next())
			
				p = Actores.load(re);
			
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return p;
        }
            public static Actores load(ResultSet filas) throws Exception{

       try {

            Actores e = new Actores();
            e.setIdactor(filas.getString("idactor"));
            e.setNombreactor(filas.getString("nombreactor"));
            e.setSexoactor(filas.getString("sexoactor"));
            e.setRolactor(filas.getString("rolactor"));
            return e;
        } catch (SQLException ex) {
            throw new Exception("Error en la Consulta del Actor:\n" + ex.getMessage());
        }

    }
    
}
